package l1j.opqlo.NewSystem.鑑定系統;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddBig;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddBlindDef;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddBlindDice;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddBoss;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddCha;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddCon;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddDarkDef;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddDex;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddDiablo;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddDmg;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddDmgX2;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddDragon;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddEarthDmg;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddEathDef;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddEathMr;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddExp;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddExtraDef;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddExtraDmg;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddFerrzeDef;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddFireDef;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddFireDmg;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddFireMr;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddFreezeDice;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddGhost;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddHP;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddHit;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddHpr;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddHumenDmg;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddInt;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddLightDef;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddMagicModDmg;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddMp;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddMpr;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddMr;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddNormal;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddSleepDef;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddSleepDice;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddSmall;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddStoneDef;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddStoneDice;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddStr;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddSturnDef;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddSturnDice;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddSustainDef;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddSustainDice;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddUndead;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddWaterDmg;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddWaterMr;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddWindDef;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddWindDmg;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddWindMr;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddWis;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddWolf;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.AddWaterDef;
import l1j.opqlo.NewSystem.鑑定系統.StstusAdd.Add等級;
import l1j.server.DatabaseFactory;
import l1j.server.server.utils.PerformanceTimer;
import l1j.server.server.utils.SQLUtil;

public class IdentifiedTable {

	private static final Logger _log = Logger.getLogger(IdentifiedTable.class.getName());

	private static IdentifiedTable _instance;

	public static IdentifiedTable getInstance() {
		if (_instance == null) {
			_instance = new IdentifiedTable();
		}
		return _instance;
	}

	private final Map<Integer, IdentifiedModel> _classList = new HashMap<Integer, IdentifiedModel>();

	private static Map<Integer, L1IdStatus> _map = new HashMap<Integer, L1IdStatus>();

	public L1IdStatus getIdentified(final int itemId) {
		return _map.get(itemId);
	}

	public IdentifiedModel getIdStatus(final int type) {

		return this._classList.get(type);
	}

	public void load() {
		try{
			this._classList.put(new Integer(1), AddStr.get());
			this._classList.put(new Integer(2), AddDex.get());
			this._classList.put(new Integer(3), AddInt.get());
			this._classList.put(new Integer(4), AddWis.get());
			this._classList.put(new Integer(5), AddCon.get());
			this._classList.put(new Integer(6), AddCha.get());
			this._classList.put(new Integer(7), AddEathMr.get());
			this._classList.put(new Integer(8), AddFireMr.get());
			this._classList.put(new Integer(9), AddWaterMr.get());
			this._classList.put(new Integer(10), AddWindMr.get());
			this._classList.put(new Integer(11), AddEarthDmg.get());
			this._classList.put(new Integer(12), AddFireDmg.get());
			this._classList.put(new Integer(13), AddWaterDmg.get());
			this._classList.put(new Integer(14), AddWindDmg.get());
			this._classList.put(new Integer(15), AddMr.get());
			this._classList.put(new Integer(16), AddExtraDmg.get());
			this._classList.put(new Integer(17), AddExtraDef.get());
			this._classList.put(new Integer(18), AddHP.get());
			this._classList.put(new Integer(19), AddHpr.get());
			this._classList.put(new Integer(20), AddMp.get());
			this._classList.put(new Integer(21), AddMpr.get());
			this._classList.put(new Integer(22), AddHit.get());
			this._classList.put(new Integer(23), AddDmg.get());
			this._classList.put(new Integer(24), AddExp.get());
			this._classList.put(new Integer(25), AddDmgX2.get());
			this._classList.put(new Integer(26), AddBig.get());
			this._classList.put(new Integer(27), AddSmall.get());
			this._classList.put(new Integer(28), AddSturnDice.get());
			this._classList.put(new Integer(29), AddStoneDice.get());
			this._classList.put(new Integer(30), AddSleepDice.get());
			this._classList.put(new Integer(31), AddFreezeDice.get());
			this._classList.put(new Integer(32), AddSustainDice.get());
			this._classList.put(new Integer(33), AddBlindDice.get());
			this._classList.put(new Integer(34), AddSturnDef.get());
			this._classList.put(new Integer(35), AddStoneDef.get());
			this._classList.put(new Integer(36), AddSleepDef.get());
			this._classList.put(new Integer(37), AddFerrzeDef.get());
			this._classList.put(new Integer(38), AddSustainDef.get());
			this._classList.put(new Integer(39), AddBlindDef.get());
			this._classList.put(new Integer(40), AddNormal.get());
			this._classList.put(new Integer(41), AddUndead.get());
			this._classList.put(new Integer(42), AddDiablo.get());
			this._classList.put(new Integer(43), AddBoss.get());
			this._classList.put(new Integer(44), AddGhost.get());
			this._classList.put(new Integer(45), AddWolf.get());
			this._classList.put(new Integer(46), AddDragon.get());
			this._classList.put(new Integer(47), Add等級.get());
			this._classList.put(new Integer(48), AddHumenDmg.get());
			this._classList.put(new Integer(49), AddEathDef.get());
			this._classList.put(new Integer(50), AddFireDef.get());
			this._classList.put(new Integer(51), AddWaterDef.get());
			this._classList.put(new Integer(52), AddWindDef.get());
			this._classList.put(new Integer(53), AddLightDef.get());
			this._classList.put(new Integer(54), AddDarkDef.get());
			this._classList.put(new Integer(55), AddMagicModDmg.get());
		}catch(Exception e){
			_log.log(Level.SEVERE, e.getLocalizedMessage() , e);
		}
	}

	public void loadTable() {
		final PerformanceTimer timer = new PerformanceTimer();
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("SELECT * FROM Z_opqlo_物品鑑定");
			rs = pstm.executeQuery();
			while (rs.next()) {

				final int ItemID = rs.getInt("ItemID");

				if (ItemID <= 0) {

					continue;
				}

				final int StatusCount = rs.getInt("屬性數量");
				if (StatusCount <= 0) {
					continue;
				}

				final L1IdStatus status = new L1IdStatus();

				status.setStatusCount(StatusCount);
				status.setChance(rs.getInt("鑑定機率"));
				status.setisRandom(rs.getInt("採隨機附值") == 1 ? true : false);
				status.setCanRestory(rs.getInt("還原") == 1 ? true : false);

				final String[] ss = { rs.getString("力量"), rs.getString("敏捷"),
						rs.getString("智慧"), rs.getString("精神"),
						rs.getString("體質"), rs.getString("魅力"),
						rs.getString("地防"), rs.getString("火防"),
						rs.getString("水防"), rs.getString("風防"),
						rs.getString("地攻"), rs.getString("火攻"),
						rs.getString("水攻"), rs.getString("風攻"),
						rs.getString("魔防"), rs.getString("破甲"),
						rs.getString("抵擋"), rs.getString("血量"),
						rs.getString("回血"), rs.getString("魔量"),
						rs.getString("回魔"), rs.getString("命中"),
						rs.getString("額外攻擊"), rs.getString("經驗加倍"),
						rs.getString("爆擊"), rs.getString("大怪增傷"),
						rs.getString("小怪增傷"), rs.getString("暈眩機率"),
						rs.getString("石化機率"), rs.getString("睡眠機率"),
						rs.getString("凍結機率"), rs.getString("支撐機率"),
						rs.getString("暗黑機率"), rs.getString("暈眩抗性"),
						rs.getString("石化抗性"), rs.getString("睡眠抗性"),
						rs.getString("凍結抗性"), rs.getString("支撐抗性"),
						rs.getString("暗黑抗性"), rs.getString("普通增傷"),
						rs.getString("不死增傷"), rs.getString("惡魔增傷"),
						rs.getString("頭目增傷"), rs.getString("亡靈增傷"),
						rs.getString("狼族增傷"), rs.getString("龍族增傷"),
						rs.getString("等級"), rs.getString("人族增傷"),
						rs.getString("地減"), rs.getString("火減"),
						rs.getString("水減"), rs.getString("風減"),
						rs.getString("光減"), rs.getString("闇減"),
						rs.getString("魔法增傷")
				};

				this.stringToInt(status, ss);
				_map.put(ItemID, status);
				status.setTotalType();
			}
		} catch (final SQLException e) {
			_log.log(Level.SEVERE,e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
		_log.info("載入鑑定系統資料數量: " + _map.size() + "(" + timer.get() + "ms)");
	}

	private void stringToInt(final L1IdStatus status, String[] ss) {
		int type = 1;
		try {
			for (final String s : ss) {

				final StringTokenizer st = new StringTokenizer(s, ":");

				final int iSize = st.countTokens();

				String sTemp = null;

				final int[] iReturn = new int[iSize];

				for (int i = 0; i < iSize; i++) {

					sTemp = st.nextToken();
					iReturn[i] = Integer.parseInt(sTemp);
				}
				if (iReturn[0] != 0) {
					// status.setStatustList(_type, iReturn);
					// status.setTypeList(_type);
					status.stringToInt(type, iReturn);
				}

				type++;
			}
		}catch(Exception e){
			_log.log(Level.SEVERE, e.getLocalizedMessage() , e);
		}
	}

}
